﻿using Newtonsoft.Json;
using RobotPratolSolution.ViewModels;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace RobotPratolSolution.Pages
{
    /// <summary>
    /// ManageDevice.xaml 的交互逻辑
    /// </summary>
    public partial class ManageDevice : UserControl
    {



        MangeDeviceViewModel mangeDeviceViewModel { get; set; } = new MangeDeviceViewModel();
        public ManageDevice()
        {
            InitializeComponent();
            this.Focus();
        }

        private void OpenGreenLight_Click(object sender, RoutedEventArgs e)
        {
            mangeDeviceViewModel.GreenLightManage(true);
        }

        private void closeGreenLight_Click(object sender, RoutedEventArgs e)
        {
            mangeDeviceViewModel.GreenLightManage(false);
        }

        private void OpenRedLight_Click(object sender, RoutedEventArgs e)
        {
            mangeDeviceViewModel.RedLightManage(true);
        }

        private void closeRedLight_Click(object sender, RoutedEventArgs e)
        {
            mangeDeviceViewModel.RedLightManage(false);
        }

        private void SpinMotorStart_Click(object sender, RoutedEventArgs e)
        {
            mangeDeviceViewModel.SpinMotorManage(true);
        }

        private void SpinMotorEnd_Click(object sender, RoutedEventArgs e)
        {
            mangeDeviceViewModel.SpinMotorManage(false);
        }

        private void MotorUp_Click(object sender, RoutedEventArgs e)
        {
            mangeDeviceViewModel.MotorManage(1);
        }

        private void MotorDown_Click(object sender, RoutedEventArgs e)
        {
            mangeDeviceViewModel.MotorManage(2);
        }

        private void MotorStop_Click(object sender, RoutedEventArgs e)
        {
            mangeDeviceViewModel.MotorManage(3);
        }

        private void AlarmBellStart_Click(object sender, RoutedEventArgs e)
        {
            mangeDeviceViewModel.AlarmBellManage(true);
        }

        private void AlarmBellStop_Click(object sender, RoutedEventArgs e)
        {
            mangeDeviceViewModel.AlarmBellManage(false);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.Focus();
            mangeDeviceViewModel.AddSubTopic();
            mangeDeviceViewModel.LoadBaseInfo();
            this.DataContext = mangeDeviceViewModel;
        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            mangeDeviceViewModel.DeleteSubTopic();
            this.DataContext = mangeDeviceViewModel;
        }

        private void WheelStateCtrlClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            string tag = button.Tag.ToString();
            mangeDeviceViewModel.WheelStateCtrl(tag);
        }



        private void MouseKey_Handler(object sender, MouseButtonEventArgs e)
        {
            if (sender is Button button && button.Command is ICommand command)
            {
                var parameter = button.CommandParameter?.ToString();

                if (e.RoutedEvent == PreviewMouseDownEvent)
                {
                    parameter = "Start" + parameter;
                    Console.WriteLine($"[{DateTime.Now}]:start=>" + parameter);
                    command.Execute(parameter);
                }
                else if (e.RoutedEvent == PreviewMouseUpEvent)
                {
                    Console.WriteLine($"[{DateTime.Now}]:stop=>" + parameter);
                    command.Execute("Stop");
                }
            }
        }

        private void DirectionKey_Handler(object sender, KeyEventArgs e)
        {

            var command = mangeDeviceViewModel.MoveCommand;
            string parameter = string.Empty;
            if (command != null)
            {
                switch (e.Key)
                {
                    case Key.Up:
                        parameter = "Up";
                        break;
                    case Key.Down:
                        parameter = "Down";
                        break;
                    case Key.Left:
                        parameter = "Left";

                        break;
                    case Key.Right:
                        parameter = "Right";

                        break;
                    case Key.Space:
                        parameter = "Stop";
                        break;
                    default:
                        return;
                }
            }
            if (e.IsUp)
            {
                parameter = "Stop";
            }
            if (!string.IsNullOrEmpty(parameter))
            {
                Console.WriteLine($"[{DateTime.Now}]:{e.Key} 111111=>" + parameter);

                if (!parameter.Equals("Stop") && mangeDeviceViewModel.ControlMode == "remote")
                {
                    parameter = "Start" + parameter;
                    command.Execute(parameter);

                }
                else
                {
                    command.Execute(parameter);
                }
                e.Handled = true;
            }


        }


    }
}
